package com.cheng.controller;

import com.cheng.common.exception.CommonException;
import com.cheng.common.results.DataRslt;
import com.cheng.common.utils.CodeUtil;
import com.cheng.data.entity.UserInfoEntity;
import com.cheng.data.vo.MenuVO;
import com.cheng.data.vo.UserInfoVO;
import com.cheng.operation.GvmtTypeInfOperation;
import com.cheng.operation.IndexOperation;
import com.cheng.operation.MenuOperation;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author: ChengJW
 * @date: 2021/11/4 7:35
 * @version: 1.0
 * @description: 首页相关处理器
 */
@Controller
public class IndexController extends BaseController{

    private final static Logger log = LoggerFactory.getLogger(IndexController.class);

    @Autowired
    private MenuOperation menuOperation;

    @Autowired
    private IndexOperation indexOperation;

    @Autowired
    private GvmtTypeInfOperation gvmtTypeInfOperation;

    /**
     * @author: ChengJW
     * @params: []
     * @return: java.lang.String
     * @date: 2021/11/04
     * @description: 账号密码验证成功后跳转到系统主页，并记录日志
     */
    @RequestMapping("/userinfo/index")
    public String index(ModelMap map){
        //用户信息
        UserInfoEntity userInfoEntity = new UserInfoEntity();
        userInfoEntity.setUrNm(getUrNm());
        UserInfoVO userInfoVO = indexOperation.queryUserInfoByUserName(userInfoEntity);
        map.addAttribute("user",userInfoVO);
        //单位名称
        String gvmtTypeNm = gvmtTypeInfOperation.queryGvmtTypeInfNmByCode(getUrCode());
        map.addAttribute("gvmtTypeNm",gvmtTypeNm);
        return "index";
    }

    /**
     * @author: ChengJW
     * @params: []
     * @return: com.cheng.common.utils.ResultData
     * @date: 2021/11/04
     * @description: 查询主页面菜单权限信息
     */
    @RequestMapping("/index/menu")
    @ResponseBody
    public DataRslt selectMenuPermInfo() throws CommonException {

        List<MenuVO> list = menuOperation.selectMenuInfo(getUrNm());
        if (list.size()>0){
            return success(list);
        }
        return failure();
    }

    /**
    * @author: ChengJW
    * @params: []
    * @return: com.cheng.common.results.DataRlst
    * @date: 2021/11/7
    * @description: 回显用户信息
    */
    @RequestMapping("/index/userInfoVO")
    @ResponseBody
    public DataRslt selectUserinfo() throws CommonException{

        UserInfoEntity userInfoEntity = new UserInfoEntity();
        userInfoEntity.setUrNm(getUrNm());
        UserInfoVO userInfoVO = indexOperation.queryUserInfoByUserName(userInfoEntity);
        return success(userInfoVO);
    }

    /**
    * @author: ChengJW
    * @params: []
    * @return: com.cheng.common.results.DataRslt
    * @date: 2021/12/26
    * @description: 退出
    */
    @RequestMapping("/index/logout")
    @ResponseBody
    public DataRslt logout() throws CommonException{
        try {
            SecurityUtils.getSubject().logout();
            return success();
        } catch (Exception e) {
            log.error("IndexController-logout:exception");
            log.error(e.getMessage(),e);
            return failure(CodeUtil.CODE_MAP.get("CJW202111010026"));
        }
    }
}
